<?php

class Acceso_RolController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        $this->initView();
        $this->view->baseUrl = $this->_request->getBaseUrl();
		$this->_options= $this->getInvokeArg('bootstrap')->getOptions();
		// Añado las archivos css y javascript necesarios para el funcionamiento de la vista
        //librerias de jqueryui
		$this->view->headLink()->appendStylesheet($this->view->baseUrl('/jquery-ui/css/start/jquery-ui-1.8.16.custom.css'));		
		$this->view->headScript()->appendFile($this->view->baseUrl('/jquery-ui/js/jquery-1.6.2.min.js'));
		$this->view->headScript()->appendFile($this->view->baseUrl('/jquery-ui/js/jquery-ui-1.8.16.custom.min.js'));
		//Validaciones
		$this->view->headLink()->appendStylesheet($this->view->baseUrl('/Validation/css/validationEngine.jquery.css'));
		$this->view->headLink()->appendStylesheet($this->view->baseUrl('/Validation/css/template.css'));		
		$this->view->headScript()->appendFile($this->view->baseUrl('/Validation/js/jquery.validationEngine-es.js'));
		$this->view->headScript()->appendFile($this->view->baseUrl('/Validation/js/jquery.validationEngine.js'));
		$this->view->headScript()->appendFile($this->view->baseUrl('/js/infacov_rol.js'));
        $this->view->headLink()->appendStylesheet($this->view->baseUrl('/css/menu-vertical.css'));
    }
	
	public function indexAction(){
		$this->view->id  = $this->getRequest()->getParam('id');
		$this->view->id_padre  = $this->getRequest()->getParam('idp');
	}
	
	public function gridAction(){
		$this->view->id  = $this->getRequest()->getParam('id');
		$this->view->id_padre  = $this->getRequest()->getParam('idp');
		
		//$this->_helper->layout->disableLayout();
        $page   = $this->_request->getParam("page",1);
        $limit  = $this->_request->getParam("rows");
        $sidx   = $this->_request->getParam("sidx",1);
        $sord   = $this->_request->getParam("sord");
 
        $tabela = new Acceso_Model_Rol();
        $agenda = $tabela->listar_rol();
 
        $count = count( $agenda );
 
        if( $count >0 ) {
            $total_pages = ceil($count/$limit);
        } else {
            $total_pages = 0;
        }
 
        if ($page > $total_pages)
            $page = $total_pages;
 
        $agenda = $tabela->fetchAll(null, "$sidx $sord", $limit, ($page*$limit-$limit));
 
        $responce->page = $page;
        $responce->total = $total_pages;
        $responce->records = $count;
        $i=0;
 
        foreach($agenda as $row) {
            $responce->rows[$i]['id']=$row->id_role;
            $responce->rows[$i]['cell']=array(
                    $row->id_role,
                    $row->nombre,
                    $row->es_rol_padre,
                    $row->log_activo
            );
            $i++;
        }
 
        $this->view->grid = $responce;
	}
	
	public function toupper($cadena)
    {
        $f = new Zend_Filter_StringToUpper(array('encoding' => 'UTF-8'));
                                 $mayus= $f->filter($cadena);
                                 return $mayus ;
    }
    
    public function ingresarrolAction()
    {
		$this->view->id  = $this->getRequest()->getParam('id');
		$this->view->id_padre  = $this->getRequest()->getParam('idp');
	   //buscar la ultima rol asignada
		$tbRol = new Acceso_Model_Rol();
        $us= $tbRol->consultar_ultimo_rol();
        $this->view->id_rol = ((int)$us->ultm_id) + 1;
    }
	
	public function agregarolAction()
    {
		 //los formularios envian sus datos a traves de POST
         //si vienen datos de post, es que el usuario ha enviado el formulario
         if ($this->getRequest()->isPost())
         {
             //REALIZAMOS EL INGRESO DE LOS DATOS			   
               $nombre  = $this->toupper($this->getRequest()->getParam('nombre'));
			   $es_rol_padre =  $this->getRequest()->getParam('es_rol_padre');
               $log_activo =  $this->getRequest()->getParam('activo');
			    //llamo a mi modelo
			    $tbRol = new Acceso_Model_Rol();
			    $tbRol->getAdapter()->beginTransaction();
        		try{
						//llamo a la accion de ingreso			   
						$tbRol->insertar($nombre,$es_rol_padre,$log_activo);
						$tbRol->getAdapter()->commit();
     	  		}catch(Exception $e ) {
	        		// Si hubo problemas. Enviamos todo marcha atras
           			$tbRol->getAdapter()->rollBack();
	        		// echo $e->getMessage();
     	  		}            
				//redirecciono a el principal
                //$this->_helper->redirector('index','ingresado');	   
         }
    } 
}

